 
#include <string.h>
#include <math.h>
#include "system.h"
#include "f1c100s_de.h"
#include "f1c100s_timer.h"
#include "f1c100s_intc.h"
#include "f1c100s_gpio.h"
#include "f1c100s_pwm.h"
#include "f1c100s_uart.h"
#include "arm32.h"
#include "time0.h"
#include "f1c100s_sdc.h"
#include "sd.h"
#include "sdcard.h"
#include "ff.h"
#include "diskio.h"
#include "fatfs.h"

#include "f1c100s_usb.h"
void usb_irq_handle();
void bsp_usb_init()
{
  usb_phy_open_clock();
  USBC_PhyConfig();
  USBC_ConfigFIFO_Base();
  USBC_EnableDpDmPullUp();
  USBC_EnableIdPullUp();
  USBC_ForceId(USBC_ID_TYPE_DEVICE);
  USBC_ForceVbusValid(USBC_VBUS_TYPE_HIGH);
}
#define USB_BUS 0
int main(void)
{
  system_init();            // Initialize clocks, mmu, cache, uart, ...
  arm32_interrupt_enable(); // Enable interrupts
  timer0_init();
  printf("\r\napp [cherry-usb-cdc] run. compiled_time %s %s\r\n", __DATE__, __TIME__);
  sdcard_init();
  fatfs_init();
  bsp_usb_init();

  extern void cdc_acm_init(uint8_t busid, uintptr_t reg_base);
  extern void cdc_acm_msc_init(uint8_t busid, uint32_t reg_base);
  // cdc_acm_msc_init(USB_BUS, USB_BASE);
  cdc_acm_init(USB_BUS, USB_BASE);
  intc_enable_irq(IRQ_USBOTG);
  intc_set_irq_handler(IRQ_USBOTG, usb_irq_handle);

  while (1)
  {
    extern void cdc_acm_data_send_with_dtr_test(uint8_t busid);
    cdc_acm_data_send_with_dtr_test(USB_BUS);
    delay(100);
   }
}

void usb_irq_handle()
{
  extern void USBD_IRQHandler(uint8_t busid);
  USBD_IRQHandler(USB_BUS);
  // printf("usb irq\r\n");
}